//
//  TrackMananger.m
//  BalanceCar
//
//  Created by 郑信鸿 on 2017/10/10.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "TrackMananger.h"
#import "RunLocationModel.h"

@interface TrackMananger()

@property(nonatomic, strong)AMapLocationManager *locationManager;


@end

@implementation TrackMananger

+ (TrackMananger *)shareManager
{
    static TrackMananger *managerInstance = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        managerInstance = [[self alloc] init];
    });
    return managerInstance;
}


-(void)startRun{
    
    [self.finishLocationArray removeAllObjects];
    self.locationManager = [[AMapLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = 10;//设置移动精度(单位:米)
    self.locationManager.locationTimeout = 2;//定位时间
    self.locationManager.allowsBackgroundLocationUpdates = YES;//开启后台定位
    [self.locationManager setLocatingWithReGeocode:YES];
    [self.locationManager startUpdatingLocation];
    self.distance = 0;
    self.startTime = [NSDate date];
}


- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode{
    
    RunLocationModel *locationModel = [[RunLocationModel alloc]init];
    locationModel.location = location.coordinate;
    locationModel.time = [NSDate date];
    locationModel.gpsStrength = [self gpsStrengthWithLocation:location];
    RunLocationModel *lastModel =locationModel;
    RunLocationModel *lastButOneModel =_finishLocationArray.lastObject;
    [self distanceWithLocation:lastModel andLastButOneModel:lastButOneModel];
    
}

//计算距离,估算误差值
-(void)distanceWithLocation:(RunLocationModel *)lastModel andLastButOneModel:(RunLocationModel *)lastButOneModel{
    
    MAMapPoint point1 = MAMapPointForCoordinate(lastModel.location);
    MAMapPoint point2 = MAMapPointForCoordinate(lastButOneModel.location);
    //2.计算距离
    CLLocationDistance newdistance = MAMetersBetweenMapPoints(point1,point2);
    
    //估算两者之间的时间差,单位 秒
    NSTimeInterval secondsBetweenDates= [lastModel.time timeIntervalSinceDate:lastButOneModel.time];
    
    //世界飞人9.97秒百米,当超过这个速度,即为误差值,可能是GPS不准
    if ((float)newdistance/secondsBetweenDates < 15.0) {
        
        [self.finishLocationArray addObject:lastModel];
       // [self mapAddCommonPolyline];//绘制运动轨迹
        self.distance  = self.distance +newdistance;
    }
}


#pragma mark ---GPS信号强弱---
- (strengthGrade)gpsStrengthWithLocation:(CLLocation *)location{
    if (location.horizontalAccuracy>200 &&location.verticalAccuracy >200) {
        
        return strengthGradeBad;
    }
    if (location.horizontalAccuracy>100 &&location.verticalAccuracy >100&&location.horizontalAccuracy<200 &&location.verticalAccuracy <200) {
        
        return strengthGradeAverage;
    }
    if (location.horizontalAccuracy>20 &&location.verticalAccuracy >20&&location.horizontalAccuracy<100 &&location.verticalAccuracy <100) {
        
        return strengthGradeBetter;
    }
    if (location.horizontalAccuracy<20 &&location.verticalAccuracy <20) {
        
        return strengthGradeBest;
    }
    
    return strengthGradeBad;
}

@end
